package com.bookmngsys.utils;

import com.bookmngsys.entity.BookInfo;

public class CommonUtils {
    static long fourHour = 3600 * 4 * 1000;
    static long oneDay = 24 * 3600 * 1000;
    public static float getFineAmount(Long returnDate){
        if(returnDate == null){
            return 0.0f;
        }
        long diff = System.currentTimeMillis() - returnDate;
        diff = diff <= 0 ? 0 : diff;
        return (float)(diff / oneDay + ((diff % oneDay) != 0 ? 1 : 0));
    }
    //如果该书被借阅，返回一个非null的Integer，否则返回null
    public static Integer getBorrowReaderId(BookInfo bookInfo){
        return (bookInfo != null && bookInfo.getBorrowReaderId() != null) ? bookInfo.getBorrowReaderId() : null;
    }
    //如果该书被预约并且有效，返回一个非null的Integer，否则返回null
    public static Integer getReserveReaderId(BookInfo bookInfo){
        return (bookInfo != null
                && bookInfo.getBorrowReaderId() == null
                && bookInfo.getReserveReaderId() != null
                && bookInfo.getReserveDate() + fourHour >= System.currentTimeMillis()) ? bookInfo.getReserveReaderId() : null;
    }
}
